package jnpf.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.mapper.VisualdevMapper;
import jnpf.base.service.DictionaryDataService;
import jnpf.base.service.VisualdevService;
import jnpf.base.model.PaginationVisualdev;
import jnpf.engine.entity.FlowEngineEntity;
import jnpf.engine.service.FlowEngineService;
import jnpf.exception.WorkFlowException;
import jnpf.permission.service.UserService;
import jnpf.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021/3/16
 */
@Service
public class VisualdevServiceImpl extends ServiceImpl<VisualdevMapper, VisualdevEntity> implements VisualdevService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CacheKeyUtil cacheKeyUtil;
    @Autowired
    private FlowEngineService flowEngineService;
    @Autowired
    private UserService userService;
    @Autowired
    private DictionaryDataService dictionaryDataService;

    @Override
    public List<VisualdevEntity> getList(PaginationVisualdev paginationVisualdev) {
        QueryWrapper<VisualdevEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtil.isEmpty(paginationVisualdev.getKeyword())) {
            queryWrapper.lambda().like(VisualdevEntity::getFullName, paginationVisualdev.getKeyword());
        }
        queryWrapper.lambda().eq(VisualdevEntity::getType, paginationVisualdev.getType());
        if (StringUtil.isNotEmpty(paginationVisualdev.getCategory())){
            queryWrapper.lambda().eq(VisualdevEntity::getCategory, paginationVisualdev.getCategory());
        }
        // 排序
        queryWrapper.lambda().orderByAsc(VisualdevEntity::getSortCode).orderByDesc(VisualdevEntity::getCreatorTime);
        Page<VisualdevEntity> page = new Page<>(paginationVisualdev.getCurrentPage(), paginationVisualdev.getPageSize());
        IPage<VisualdevEntity> userPage = this.page(page, queryWrapper);
        return paginationVisualdev.setData(userPage.getRecords(), page.getTotal());
    }


    @Override
    public List<VisualdevEntity> getList() {
        QueryWrapper<VisualdevEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(VisualdevEntity::getSortCode).orderByDesc(VisualdevEntity::getCreatorTime);
        return this.list(queryWrapper);
    }

    @Override
    public VisualdevEntity getInfo(String id) {
        QueryWrapper<VisualdevEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VisualdevEntity::getId, id);
        return this.getOne(queryWrapper);
    }


    @Override
    public void create(VisualdevEntity entity) {
        if (StringUtil.isEmpty(entity.getId())){
            entity.setId(RandomUtil.uuId());
        }
        entity.setSortCode(RandomUtil.parses());
        if (StringUtil.isNotEmpty(entity.getWebType()) && entity.getWebType().equals("3")){
            entity.setFlowId(entity.getId());
            if (entity.getType().equals(1) || entity.getType().equals(2)){
            FlowEngineEntity flowEngineEntity = new FlowEngineEntity();
            flowEngineEntity.setId(entity.getId());
            flowEngineEntity.setFlowTemplateJson(entity.getFlowTemplateJson());
            flowEngineEntity.setEnCode("visualDev"+entity.getEnCode());
            flowEngineEntity.setType(1);
            flowEngineEntity.setIcon("icon-ym icon-ym-node");
            flowEngineEntity.setIconBackground("#008cff");
            flowEngineEntity.setEnabledMark(1);
            String fullName = userService.getInfo(entity.getCreatorUser()) + "的" + entity.getFullName();
            flowEngineEntity.setFullName(fullName);
            flowEngineEntity.setCategory(dictionaryDataService.getInfo(entity.getCategory()).getEnCode());
            flowEngineEntity.setFormType(2);
            flowEngineEntity.setTables(entity.getTables());
            flowEngineEntity.setDbLinkId(entity.getDbLinkId());
            flowEngineEntity.setFormData(entity.getFormData());
            flowEngineService.create(flowEngineEntity);
            entity.setFlowId(flowEngineEntity.getId());
            }
        }
        this.save(entity);
    }

    @Override
    public boolean update(String id, VisualdevEntity entity) {
        entity.setId(id);
        String redisKey = cacheKeyUtil.getVisiualData() + id;
        if (redisUtil.exists(redisKey)) {
            redisUtil.remove(redisKey);
        }
        //关联流程更新
        if (entity.getWebType() != null && entity.getWebType().equals("3")) {
            entity.setFlowId(id);
            if (entity.getType().equals(1) || entity.getType().equals(2)) {
                try {
                    VisualdevEntity visualdevEntity = this.getInfo(id);
                    FlowEngineEntity flowEngineEntity;
                    //判断是否关联流程id
                    if (visualdevEntity.getFlowId() == null) {
                        flowEngineEntity = new FlowEngineEntity();
                        flowEngineEntity.setId(id);
                        flowEngineEntity.setFlowTemplateJson(entity.getFlowTemplateJson());
                        flowEngineEntity.setEnCode("visualDev" + entity.getEnCode());
                        flowEngineEntity.setType(1);
                        flowEngineEntity.setIcon("icon-ym icon-ym-node");
                        flowEngineEntity.setIconBackground("#008cff");
                        flowEngineEntity.setEnabledMark(1);
                        flowEngineEntity.setFormType(2);
                        flowEngineEntity.setTables(entity.getTables());
                        flowEngineEntity.setDbLinkId(entity.getDbLinkId());
                        flowEngineEntity.setFormData(entity.getFormData());
                        flowEngineService.create(flowEngineEntity);
                        entity.setFlowId(flowEngineEntity.getId());
                    }
                    flowEngineEntity = flowEngineService.getInfo(visualdevEntity.getFlowId());
                    flowEngineEntity.setFlowTemplateJson(entity.getFlowTemplateJson());
                    flowEngineEntity.setTables(entity.getTables());
                    flowEngineEntity.setDbLinkId(entity.getDbLinkId());
                    flowEngineEntity.setFormData(entity.getFormData());
                    flowEngineService.update(visualdevEntity.getFlowId(), flowEngineEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
        }
        return this.updateById(entity);
    }

    @Override
    public void delete(VisualdevEntity entity) {
        if (entity != null) {
            if(StringUtil.isNotEmpty(entity.getFlowId())){
                FlowEngineEntity flowEngineEntity = flowEngineService.getById(entity.getFlowId());
                if (flowEngineEntity!=null){
                    flowEngineService.delete(flowEngineEntity);
                }
            }
            this.removeById(entity.getId());
        }
    }

    @Override
    public Boolean getObjByEncode(String encode) {
        QueryWrapper<VisualdevEntity> visualWrapper = new QueryWrapper<>();
        visualWrapper.lambda().eq(VisualdevEntity::getEnCode,encode);
        Integer count = this.baseMapper.selectCount(visualWrapper);
        if (count>0){
            return false;
        }else {
            return true;
        }
    }


}
